
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.tool.wlfacade.helpers;

import oxygen.tool.facade.FacadeBenignException;
import oxygen.tool.wlfacade.WLConnectionData;
import oxygen.tool.wlfacade.WLFacadeContextHelper;
import oxygen.tool.wlfacade.WLScriptHelper;

public class WLControl {
  
  protected static void ensureNotConnected(WLFacadeContextHelper wlctx) throws Exception { 
    if(wlctx.getFacadeContext().isFlagSet(WLFacadeContextHelper.CONNECTED)) {
      throw new FacadeBenignException("You are already connected. Need to disconnect first");
    }
  }
  
  public static void disconnect(WLFacadeContextHelper wlctx) throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    wlctx.disconnect();
    System.out.println("Disconnected");
  }
  
  public static void connect(WLFacadeContextHelper wlctx, WLConnectionData wlconn) 
    throws Exception {
    ensureNotConnected(wlctx);
    System.out.println("Connecting to weblogic server instance at '" + wlconn.url + "' as '"+wlconn.username+"' ...");
    wlctx.connectTo(wlconn);
  }
  
  public static void connect(WLFacadeContextHelper wlctx, String url, String username, String password) 
    throws Exception {
    WLConnectionData wlconn = new WLConnectionData(url, username, password);
    connect(wlctx, wlconn);
  }
  
  public static void connect(WLFacadeContextHelper wlctx) 
    throws Exception {
    connect(wlctx, wlctx.getWLConnectionData());
  }

  
}

